package com.ruoyi.project.wework;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.project.wework.apimodel.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Component
public class WeworkFacade {
    String userListUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist";
    public UserModel getUserListByDeptId(String accessToken, String departmentId) {
        String params = "access_token=" + accessToken + "&department_id=" + departmentId;
        String res = HttpUtils.sendGet(userListUrl, params);
        if(StringUtils.isBlank(res)) {
            throw new CustomException("查询部门员工列表请求返回值为空", -1);
        }
        UserModel model = JSON.parseObject(res, UserModel.class);
        return model;
    }



    String deptUrl = "https://qyapi.weixin.qq.com/cgi-bin/department/list";
    /**
     * 获取全部组织架构
     * @param accessToken
     * @return
     */
    public DepartmentModel getDeptListModel(String accessToken) {
        String id = null;
        String params = "access_token=" + accessToken;
        if(id != null) {
            params +="&id=" + id;
        }
        String res = HttpUtils.sendGet(deptUrl, params);


        if(StringUtils.isBlank(res)) {
            throw new CustomException("获取部门列表请求返回值为空", -1);
        }
        DepartmentModel departmentModel = JSON.parseObject(res, DepartmentModel.class);
        return departmentModel;
    }


    String accessTokenUrl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";
    public String requestAccessToken(String corpid, String corpsecret) {
        String params = "corpid=" + corpid + "&corpsecret=" + corpsecret;
        String accessToken = HttpUtils.sendSSLPost(accessTokenUrl, params);
        return accessToken;
    }


    private String userIdUrl = "https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo";
    public UserIdModel getWeworkUserIdByCode(String accessToken, String code) {
        String params = "access_token=" + accessToken + "&code=" + code;
        String res = HttpUtils.sendGet(userIdUrl, params);
        if(StringUtils.isBlank(res)) {
            throw new CustomException("code 换 userId 返回异常", -1);
        }
        UserIdModel model = JSON.parseObject(res, UserIdModel.class);
        return model;
    }

    private String userIdToOpenIdUrl = "https://qyapi.weixin.qq.com/cgi-bin/user/convert_to_openid?access_token=";
    public UserIdToOpenIdModel getOpenIdByUserId(String accessToken, String userId) {
        userIdToOpenIdUrl += accessToken;

        JSONObject param = new JSONObject();
        param.put("userid", userId);

        String res = HttpUtils.sendPost(userIdUrl, param);
        if(StringUtils.isBlank(res)) {
            throw new CustomException("userId 换 openId 返回异常", -1);
        }
        UserIdToOpenIdModel model = JSON.parseObject(res, UserIdToOpenIdModel.class);
        return model;
    }

    private String getIpUrl = "https://qyapi.weixin.qq.com/cgi-bin/get_api_domain_ip";
    public GetDomainIpModel getApiDomainIp(String accessToken) {
        String param = "access_token=" + accessToken;
        String res = HttpUtils.sendGet(getIpUrl, param);
        if(StringUtils.isBlank(res)) {
            throw new CustomException("getApiDomainIp 返回异常", -1);
        }
        GetDomainIpModel model = JSON.parseObject(res, GetDomainIpModel.class);
        return model;
    }
}
